﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Platform.LanguageProcessing.Expressions;

namespace Platform.LanguageProcessing
{

	// Responsável por verificar a concordancia
	/// <summary>
	/// Responsável por verfiicar se a frase está a sintaxe está correta
	/// </summary>
	public class SyntaxAnalyzer
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		protected SubjectExpression FindSubject( ParserContext context )
		{
			LinkedListNode<GrammarExpression> startFrom = null;
			LinkedListNode<GrammarExpression> endFrom = null;
			var stack = context.Expressions;
			var cNode = stack.Dig( 0 );

			// 
			while ( cNode != null )
			{
				switch ( cNode.Value.Class )
				{
					case GrammarClass.Subject:
						return ( SubjectExpression )cNode.Value;

					case GrammarClass.Verb:
						startFrom 
							= endFrom
							= cNode.Previous;
						break;

					case GrammarClass.Sentence:
						// Sujeito oculto
						cNode = null;
						break;

					default:
						endFrom = cNode;
						break;
				}
				cNode = cNode.Previous;
			}

			if ( startFrom != null
					&& endFrom != null )
			{
				var subject = new SubjectExpression( stack.GetTopToBottom( startFrom, endFrom ) );
				stack.Replace( startFrom, endFrom, subject );
				return subject;
			}
			else
			{
				// Sujeito oculto
				throw new NotImplementedException();
			}
		}

		/// <summary>
		/// Realiza a concordância verbal
		/// </summary>
		/// <param name="context"></param>
		protected void CheckPerson( ParserContext context )
		{

		}


		public virtual void Analyze( string prefix, ParserContext context )
		{
			var stack = context.Expressions;
			switch ( stack.Top.Class )
			{
				case GrammarClass.Verb:
					if ( stack.Previous.Class != GrammarClass.Verb )
					{
						this.FindSubject( context );
						this.CheckPerson( context );
					}
					break;
				case GrammarClass.Pronoun:
					break;
				case GrammarClass.Noun:
					break;
				case GrammarClass.Adjective:
					break;
				case GrammarClass.Adverb:
					break;
				case GrammarClass.Preposition:
					break;
				case GrammarClass.Interjection:
					break;
				case GrammarClass.Conjuction:
					break;
				case GrammarClass.Sentence:
					break;
				default:
					break;
			}
		}
	}

}
